#include<bits/stdc++.h>
using namespace std;

string

string multi(string a, string b)
{
	
}

int main()
{
	string s;
	int k, a, b;
	cin>>s>>k;
	map<pair<int, int>, bool> rule;
	unordered_map<int, int> mp;
	for(auto i:s)
		mp[i-'0']++;
		
	string ans;
	for(int i = 1;i <= k;i++)
	{
		cin>>a>>b;
		if(rule.count({a, b}))
			continue;
		else
			rule[a][b] = true;
		int cnt = 0;
		for(auto j:mp)
			if(j == a)
				cnt++;
		if(i == 1)
			ans = to_string(cnt);
		else
			ans = multi(ans, cnt);
	}
	cout<<ans;
	return 0;
}

